package org.zjframework.traceframe.common.config.mp;

import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInRedisConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.zjframework.traceframe.common.handler.SubscribeHandler;

/**
 * Created by John on 2017/11/21.
 */
@Component
public class WechatMpConfig {

    @Autowired
    private WechatMpAccountConfig wechatMpAccountConfig;

    @Autowired
    private SubscribeHandler subscribeHandler;

    @Bean
    public ApiConfig apiConfig(){
        ApiConfig apiConfig = new ApiConfig(wechatMpAccountConfig.getMpAppId(),wechatMpAccountConfig.getMpAppSecret());
        return apiConfig;
    }

    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatMpAccountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(wechatMpAccountConfig.getMpAppSecret());
        wxMpConfigStorage.setToken(wechatMpAccountConfig.getMpAppToken());
        return wxMpConfigStorage;
    }

    @Bean
    public WxMpMessageRouter wxMpMessageRouter(){
        final WxMpMessageRouter wxMpMessageRouter = new WxMpMessageRouter(this.wxMpService());
        // 自定义菜单事件
//        wxMpMessageRouter.rule().async(false).msgType(WxConsts.EVENT)
//                .event(WxConsts.EVT_CLICK).handler(this.getMenuHandler()).end();

        // 点击菜单连接事件
//        wxMpMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//                .event(MenuButtonType.VIEW).handler(this.nullHandler).end();

        // 关注事件
        wxMpMessageRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
                .event(WxConsts.EVT_SUBSCRIBE).handler(subscribeHandler)
                .end();

        // 取消关注事件
//        wxMpMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//                .event(EventType.UNSUBSCRIBE)
//                .handler(this.getUnsubscribeHandler()).end();

        // 上报地理位置事件
//        wxMpMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//                .event(EventType.LOCATION).handler(this.getLocationHandler())
//                .end();

        // 接收地理位置消息
//        wxMpMessageRouter.rule().async(false).msgType(XmlMsgType.LOCATION)
//                .handler(this.getLocationHandler()).end();

        // 扫码事件
//        wxMpMessageRouter.rule().async(false).msgType(XmlMsgType.EVENT)
//                .event(EventType.SCAN).handler(this.getScanHandler()).end();

        // 默认
        //wxMpMessageRouter.rule().async(false).handler(this.getMsgHandler()).end();
        return wxMpMessageRouter;
    }

    @Bean
    public WxMpInRedisConfigStorage wxMpInRedisConfigStorage(){
        //WxMpInRedisConfigStorage wxMpInRedisConfigStorage = new WxMpInRedisConfigStorage();
        //TODO
        return null;
    }

}
